<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var product_id = '{{.product.Id}}';
var columns = [{"data" : "id", "sWidth":"50px", "bSortable": false, "sTitle":'<input type="checkbox" class="selectAll"/>', render:function (data, type, full, meta) {
        return '<input type="checkbox" class="selectOne" value="'+ full.id +'" />';
    }}];
columns.push({"data" : "model", "bSortable": false, "sTitle":"型号"});
columns.push({"data" : "color", "bSortable": false, "sTitle":"色号"});
columns.push({"data" : "size", "bSortable": false, "sTitle":"尺寸"});
columns.push({"data" : "film_color", "bSortable": false, "sTitle":"膜层颜色"});
columns.push({"data" : "function_info", "bSortable": false, "sTitle":"功效"});
columns.push({"data" : "genre", "bSortable": false, "sTitle":"框型"});

var tables = $('#dataTables').DataTable({
    "sAjaxSource" : '{{ urlfor "ProductsController.PriceSnd" }}'
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        params.product_id = Number(product_id);
        params.department_id = Number($('#DepartmentId').val());
        $.post(sSource, JSON.stringify(params),function(resp) {
            fnCallback(resp);
        },'json')
    }
    ,order:[]
    ,columns: columns
    ,"aLengthMenu":[20, 50, 100, 200]
    ,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
});
$("#formtable").validator({
    submit: function() {
        if(this.isFormValid()) {
            var TradePrice = $('#TradePrice').val();
            var SuggestedPrice = $('#SuggestedPrice').val();
            var DepartmentId = $('#DepartmentId').val();
            var product_id = $('#product_id').val();
            var price_id = $('#price_id').val();
            var snd_id = [];
            $.each($('.selectOne'), function (idx, value) {
                if($(this).prop('checked')) {
                    snd_id.push($(this).val());
                }
            });
            if(snd_id.length <= 0) {
                layer.alert("请选择需要设定的参数");return false;
            }
            $.post('{{ urlfor "ProductsController.PriceAddSnd" }}', {'snd_id':snd_id, 'product_id':product_id, 'price_id': price_id,'TradePrice':TradePrice, 'SuggestedPrice':SuggestedPrice, 'DepartmentId':DepartmentId}, function(res){
                if(res.code == 1) {
                    layer.alert(res.msg);return false;
                }
                parent.layer.closeAll();
                parent.dialogReback();
            }, 'json');
        }
        return false;
    }
});
//设置门店列表
get_manage_departments(function (d) {
    var opt = [];
    if (d.length > 0) {
        $.each(d, function (idx, val) {
            opt.push('<option value="' + val.Id + '">' + val.Name + '</option>');
        });
    }
    $('#DepartmentId').html(opt.join('')).val({{.department.Id}});
});
$('#DepartmentId').change(function () {
    tables.ajax.reload();
});
$('.selectAll').click(function(){
    if($(this).prop('checked') != false) {
        $('.selectOne').prop('checked', true);
    } else {
        $('.selectOne').prop('checked', false);
    }
});
</script>